#!/home/y/bin/perl

use FindBin qw/$Bin/;
use lib qw(lib ../lib);

use POSIX qw(strftime);
use Test::More qw/no_plan/;
use_ok( "Farsail::Util", qw/:all/ );

can_ok(__PACKAGE__, qw(check_date parse_date expand_file) );

my @dates = ('20081001', '2008-10-01', 'yesterday', 'today');
my @should_be = ('2008-10-01', '2008-10-01',
                 date('%Y-%m-%d', -1),  date('%Y-%m-%d'));
for ( 0..$#dates ) {
    my $date = parse_date( $dates[$_] );
    ok(check_date($dates[$_]), "check_date('$dates[$_]')");
    ok( $date->isa('DateTime')
            && $date->ymd eq $should_be[$_],
        "parse_date('$dates[$_])'");
}

my @not_date = ('20081032', '2008-10-32', 'wrong date');
for ( @not_date ) {
    ok(!check_date($_), "check_date('$_') should failed");
}

my $file;
$file = expand_file('user', '/home');
ok($file->isa('Path::Class::File')
       && $file eq '/home/user',
   'expand_file($rel_path, $dir)');

$file = expand_file('/home/user', '/tmp');
ok($file->isa('Path::Class::File')
       && $file eq '/home/user',
   'expand_file($abs_path, $dir)');

$file = expand_file('file', 'user', '/home');
ok($file->isa('Path::Class::File')
       && $file eq '/home/user/file',
   'expand_file($rel_path, $subdir, $dir)');

sub date {
    my $fmt = shift;
    my $offset = shift || 0;
    my $ref = shift || time;
    my $time = $ref + int($offset) * 86400;
    return strftime($fmt, localtime($time));
}

